package com.graduation.smartparkingplatform.ServiceImpl;

import com.alipay.api.AlipayApiException;
import com.graduation.smartparkingplatform.Dao.AlipayMapper;
import com.graduation.smartparkingplatform.Service.PayService;
import com.graduation.smartparkingplatform.Utils.Alipay;
import com.graduation.smartparkingplatform.Utils.AlipayBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Service
public class PayServiceImpl implements PayService {

  @Autowired private AlipayMapper alipayMapper;
  @Autowired private Alipay alipay;

  @Override
  public String aliPay(AlipayBean alipayBean) throws AlipayApiException {
    return alipay.pay(alipayBean);
  }

  @Override
  public Boolean aliPay_record(
      String outTradeNo, String productCode, String subject, String totalAmount, String body) {
    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());
    // 将支付记录写入数据库
    return alipayMapper.aliPay_record(outTradeNo, productCode, subject, totalAmount, body, nowTime);
  }

  // 修改数据库支付状态
  @Override
  public Boolean aliPay_record_change(String outTradeNo) {

    return alipayMapper.aliPay_record_change(outTradeNo);
  }

  // 判断支付是否成功
  @Override
  public Boolean alipayStatus(String outTradeNo) {

    return alipayMapper.alipayStatus(outTradeNo);
  }
}
